package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import com.recall.cache.aspect.annotation.Cacheable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private ItemService itemService;

    //Request URL: http://localhost:8500/api/search/albumInfo/channel/1
    @GetMapping("/channel/{c1Id}")
    @Operation(summary = "频道页数据展示")
    public Result channel(@PathVariable("c1Id") Long c1Id) {
        // 是一个7个map的集合
        // Map的key：baseCategory--->三级分类对象
        // Map的key：list--->三级分类下热度值比较高的6张专辑【list】
        List<Map<String, Object>> mapList = searchService.channel(c1Id);
        return Result.ok(mapList);
    }

    @PostMapping
    @Operation(summary = "带条件的搜索专辑")
    public Result search(@RequestBody AlbumIndexQuery albumIndexQuery) {
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }

    //Request URL: http://localhost:8500/api/search/albumInfo/completeSuggest/keyword
    @GetMapping("/completeSuggest/{keyword}")
    @Operation(summary = "搜索建议")
    public Result completeSuggest(@PathVariable("keyword") String keyword) {
        Set<String> set = searchService.completeSuggest(keyword);
        return Result.ok(set);
    }

    //Request URL: http://localhost:8500/api/search/albumInfo/751
    @GetMapping("/{albumId}")
    @Operation(summary = "根据专辑id查询专辑详情")
    @Cacheable(cacheKey = RedisConstant.CACHE_INFO_PREFIX + "#{#args[0]}",
            lockKey = RedisConstant.ALBUM_LOCK_SUFFIX + "#{#args[0]}",
            bloomKey = "#{#args[0]}",
            enableBloomFilter = true,
            enableLock = true
    )
    public Result getAlbumInfo(@PathVariable(value = "albumId") Long albumId) {
        Map<String, Object> result = itemService.getAlbumInfo(albumId);
        return Result.ok(result);
    }

    //Request URL: http://localhost:8500/api/search/albumInfo/findRankingList/1/hotScore
    @GetMapping("/findRankingList/{c1Id}/{sortField}")
    @Operation(summary = "排行榜")
    public Result findRankingList(@PathVariable(value = "c1Id") Long c1Id, @PathVariable(value = "sortField") String sortField) {
        List<AlbumInfoIndexVo> albumInfoIndexVoList = itemService.findRankingList(c1Id, sortField);
        return Result.ok(albumInfoIndexVoList);
    }

}

